import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class Main4 {
    //4）将这10个对象按对象存入文件中并读出到一List集合中。
    public static void main(String[] args)throws Exception {
        Book[] books=new Book[10];
        books[0]=new Book(1,"a","z");
        books[1]=new Book(2,"b","q");
        books[2]=new Book(3,"c","s");
        books[3]=new Book(4,"d","l");
        books[4]=new Book(5,"e","z");
        books[5]=new Book(6,"f","w");
        books[6]=new Book(7,"g","z");
        books[7]=new Book(8,"h","w");
        books[8]=new Book(9,"i","y");
        books[9]=new Book(10,"j","l");
        ObjectOutputStream outputStream=new ObjectOutputStream(new FileOutputStream("d:/test4.txt"));
        for (int i = 0; i <books.length ; i++) {//写入
            outputStream.writeObject(books[i]);
        }

        ObjectInputStream objectInputStream=new ObjectInputStream(new FileInputStream("d:/test4.txt"));
        List<Book> list=new ArrayList<>();

        try {//读出到集合中
            while (true){
                Book book= (Book) objectInputStream.readObject();
                list.add(book);
        }
        } catch (EOFException e) {
           e.printStackTrace();
        }
        for (Book book:list){
            System.out.println(book);
        }




    }
}
